/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siro.pm.ui.login;

import com.siro.pm.Constants;
import com.siro.pm.I18nManager;
import com.siro.pm.MainWindow;
import com.siro.pm.PmApplication;
import com.siro.pm.model.Developer;
import com.vaadin.event.FieldEvents.FocusEvent;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;

public class LoginPage extends GridLayout {
	private static final long serialVersionUID = -8382742794370034929L;

	TextField text_Name;
	PasswordField text_Password;
	Label errorInfo;

	public LoginPage() {
		addStyleName(Constants.STYLE_LOGIN_PAGE);
		setColumns(2);
		setRows(7);
		setMargin(true);
		setSpacing(true);

		Embedded logo = new Embedded("", new ThemeResource(
				"../siropm/img/logo_normal.png"));
		addComponent(logo, 0, 0);
		setComponentAlignment(logo, Alignment.TOP_LEFT);

		errorInfo = new Label();
		errorInfo.setVisible(false);
		addComponent(errorInfo, 0, 1);

		text_Name = new TextField(I18nManager.getMessage("login.username"));
		text_Name.focus();
		text_Name.addListener(new ClearErrorInfoListener());
		addComponent(text_Name, 0, 4);
		setComponentAlignment(text_Name, Alignment.TOP_LEFT);

		text_Password = new PasswordField(I18nManager.getMessage("login.password"));
		text_Password.addListener(new ClearErrorInfoListener());
		addComponent(text_Password, 0, 5);
		setComponentAlignment(text_Password, Alignment.TOP_LEFT);

		Button button_Login = new Button(I18nManager.getMessage("login.button"));
		button_Login.setWidth(135, UNITS_PIXELS);
		button_Login.addListener(new loginConfirmListener());
		addComponent(button_Login, 0, 6);
		setComponentAlignment(button_Login, Alignment.TOP_LEFT);

	}

	class loginConfirmListener implements Button.ClickListener {
		private static final long serialVersionUID = 6941818198307928823L;

		public void buttonClick(ClickEvent event) {
			String userName = (String) text_Name.getValue();
			String password = (String) text_Password.getValue();
			LoginHandler handler = new LoginHandler();
			Developer developer = handler.authenticate(userName, password);
			if (developer != null) {
				PmApplication.getDefault().setUser(developer);
				MainWindow mainWindow = (MainWindow) PmApplication.getDefault()
						.getMainWindow();
				mainWindow.showMainPage();
			} else {
				errorInfo.setVisible(true);
				errorInfo.setValue(I18nManager
						.getMessage("login.failed.invalid"));
			}
		}
	}

	class ClearErrorInfoListener implements FocusListener {

		private static final long serialVersionUID = -7147600881739588681L;

		public void focus(FocusEvent event) {
			errorInfo.setValue("");
			errorInfo.setVisible(false);
		}

	}
}
